package com.wantupai.translate.base;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wantupai.translate.App;
import com.wantupai.translate.R;

import butterknife.ButterKnife;

/**
 * @author luxuchang
 * @date 2019/4/24 0024
 * @description
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout llBack;
    private TextView mTitle;
    private ImageView ivRight;
    private TextView tvRight;
    private TextView tvLeft;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initSystemBarTint();

        setContentView(getLayoutId());
        App.getInstance().addActivityList(this);
        ButterKnife.bind(this);
        initViews();
        initListener();
        initData();
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        llBack = findViewById(R.id.ll_back);
        mTitle = findViewById(R.id.title);

        if (mTitle != null) {
            mTitle.setText(title);
        }
        if (llBack != null) {
            llBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    /**
     * 设置标题栏右边按钮或文字
     *
     * @param resId       资源图片id
     * @param showRightTv 是否显示右侧文字
     * @param showRightTv 是否显示右侧文字
     * @param rightText   右侧文字内容
     */
    public void setRightImageView(int resId, boolean showRightTv, boolean showRightBtn, int rightBtnResId, String rightText) {
        ivRight = findViewById(R.id.iv_right);
        tvRight = findViewById(R.id.tv_right);
        if (showRightTv) {
            tvRight.setVisibility(View.VISIBLE);
            ivRight.setVisibility(View.INVISIBLE);
            tvRight.setText(rightText);
        } else {
            tvRight.setVisibility(View.INVISIBLE);
        }

        if (showRightBtn) {
            ivRight.setVisibility(View.VISIBLE);
            ivRight.setBackgroundResource(rightBtnResId);
            tvRight.setVisibility(View.INVISIBLE);
        } else {
            ivRight.setVisibility(View.INVISIBLE);
        }
        if (ivRight != null)
            ivRight.setBackgroundResource(resId);
    }

    /**
     * 隐藏返回按钮
     */
    public void hideLeftBackBtn() {
        llBack = findViewById(R.id.ll_back);
        llBack.setVisibility(View.INVISIBLE);
    }

    public void setLeftRightText(String leftText, String title, String rightText) {
        ivRight = findViewById(R.id.iv_right);
        llBack = findViewById(R.id.ll_back);
        llBack.setVisibility(View.INVISIBLE);
        ivRight.setVisibility(View.INVISIBLE);
        tvLeft = findViewById(R.id.tv_left);
        tvLeft.setText(leftText);
        setTitle(title);
        tvRight = findViewById(R.id.tv_right);
        tvRight.setText(rightText);
    }

    //  获取布局id(setContentView)
    public abstract int getLayoutId();

    //  初始化view
    public abstract void initViews();

    //  初始化点击事件
    public abstract void initListener();

    //  初始化数据
    public abstract void initData();

    //  处理具体的点击事件
    public abstract void processClick(View v);

    public void onClick(View v) {
        processClick(v);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.getInstance().removeActivity(this);
    }

    /**
     * 子类可以重写改变状态栏颜色
     */
    protected int setStatusBarColor() {
        return getColorPrimary();
    }

    /**
     * 子类可以重写决定是否使用透明状态栏
     */
    protected boolean translucentStatusBar() {
        return false;
    }

    /**
     * 设置状态栏颜色
     */
    protected void initSystemBarTint() {
        Window window = getWindow();
        if (translucentStatusBar()) {
            // 设置状态栏全透明
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
            return;
        }
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0以上使用原生方法
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(setStatusBarColor());
        }
    }

// 获取主题色

    public int getColorPrimary() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        return typedValue.data;
    }
}
